package com.kaduoxee.ebooks.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kaduoxee.ebooks.entity.Chapter;
import com.kaduoxee.ebooks.entity.ChapterContent;
import com.kaduoxee.ebooks.entity.Fiction;
import com.kaduoxee.ebooks.service.impl.ChapterServiceImpl;
import com.kaduoxee.ebooks.service.impl.ContentServiceImpl;
import com.kaduoxee.ebooks.service.impl.FictionServiceImpl;
import com.kaduoxee.ebooks.util.JsoupUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("webSpider")
public class FictionController {
    @Resource
    private FictionServiceImpl fictionService;
    @Resource
    private ChapterServiceImpl chapterService;
    @Resource
    private ContentServiceImpl contentService;

    @GetMapping
    public String index(Model model) {
        model.addAttribute("list", fictionService.list());
        return "fiction/spider";
    }

    @ResponseBody
    @PostMapping("writeOne")
    public String writeOneFiction(String url) {
        try {
            Document doc = Jsoup.connect(url).get();
            Fiction fiction = JsoupUtil.writeFiction(doc);
            fictionService.save(fiction);
            System.out.println(fiction);
            chapterService.saveChaptersAndContents(doc, fiction.getId());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "success";
    }


    @ResponseBody
    @PostMapping("/updateFic")
    public String update(String url) {
        try {
            Document doc = Jsoup.connect(url).get();

            Fiction fiction = fictionService.getLFicByUrl(url);
            String dbNewest = fiction.getNewest();
            String newest = Objects.requireNonNull(doc.selectFirst(":containsOwn(最新章节：)")).child(0).text();
            fictionService.updateFicNewest(newest, fiction.getId());

            Element aEl = doc.selectFirst(":containsOwn(" + dbNewest + ")");
            Integer sort = chapterService.newestOfFic(fiction.getId());

            if (aEl != null && aEl.parent() != null) {
                List<Element> dList = aEl.parent().nextElementSiblings();
                for (Element dd : dList) {
                    Element a = dd.child(0);
                    ChapterContent chapterContent = JsoupUtil.writeContent(a.attr("href"));
                    contentService.save(chapterContent);

                    Chapter chapter = JsoupUtil.writeChapter(a, fiction.getId(), ++sort, chapterContent.getId());
                    chapterService.save(chapter);
                    System.out.println("爬取了第" + (sort) + "章");
                }
            }else {
                System.out.println("小说"+fiction.getBookName()+"已经更新到最新章节了");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "success";
    }
}
